package ui.fragment;

import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AddSignTwo;
import com.fy.fyzf.bean.PassengerNumberBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.presenter.AddSignPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.AddSignCallback;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.activity.SignStepActivity;
import ui.adapter.CheckBoxAdapter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StepTwoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class StepTwoFragment extends BaseFragment<AddSignPresenter> implements AddSignCallback {

    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_pay_type)
    EditText tvPayType;
    @BindView(R.id.tv_jiaofang_time)
    TextView tvJiaofangTime;
    @BindView(R.id.tv_rent)
    TextView tvRent;
    @BindView(R.id.et_owner_name)
    EditText etOwnerName;
    @BindView(R.id.et_contacts)
    EditText etContacts;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.tv_last_step)
    TextView tvLastStep;
    @BindView(R.id.tv_next)
    TextView tvNext;

    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "customersId";
    private static final String ARG_PARAM2 = "performanceId";
    private static final String TYPE = "type";

    // TODO: Rename and change types of parameters
    private Integer clinchDealType = 1; //签约成交类型  默认第一个
    private Integer customersId;
    private Integer performanceId;
    private SignStepActivity activity;
    private int signType;
    private CheckBoxAdapter checkBoxAdapter;

    private AddSignTwo addSignTwo = new AddSignTwo();
    public StepTwoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static StepTwoFragment newInstance(Integer param1, Integer param2,int i) {
        StepTwoFragment fragment = new StepTwoFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM2, param2);
        args.putInt(TYPE, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            customersId = getArguments().getInt(ARG_PARAM1);
            performanceId = getArguments().getInt(ARG_PARAM2);
            signType = getArguments().getInt(TYPE);
        }
    }


    @Override
    protected AddSignPresenter createPresenter() {
        return new AddSignPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_step_two;
    }



    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tv_start_time,R.id.tv_jiaofang_time,R.id.tv_date, R.id.tv_end_time, R.id.tv_last_step,R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_date:
                showDateDialog(tvDate, 1);
                break;
            case R.id.tv_jiaofang_time:
                showDateDialog(tvJiaofangTime, 2);

                break;
            case R.id.tv_start_time:
                showDateDialog(tvStartTime, 3);
                break;
            case R.id.tv_end_time:
                showDateDialog(tvEndTime, 4);
                break;
            case R.id.tv_last_step:
                SignStepActivity   activity = (SignStepActivity) getActivity();
                activity.initFragment1();
                break;
            case R.id.tv_next:
                if (TextUtils.isEmpty(tvDate.getText().toString())) {
                    AppUtils.showToast("请选择日期");
                    return;
                }
                if (TextUtils.isEmpty(tvPayType.getText().toString())) {
                    AppUtils.showToast("请输入支付方式");
                    return;
                }
                if (TextUtils.isEmpty(tvJiaofangTime.getText().toString())) {
                    AppUtils.showToast("请选择交房日期");
                    return;
                }
                if (TextUtils.isEmpty(tvRent.getText().toString())) {
                    AppUtils.showToast("请输入租金");
                    return;
                }
                if (TextUtils.isEmpty(tvStartTime.getText().toString())) {
                    AppUtils.showToast("请选择开始时间");
                    return;
                }
                if (TextUtils.isEmpty(tvEndTime.getText().toString())) {
                    AppUtils.showToast("请选择结束时间");
                    return;
                }
                if (TextUtils.isEmpty(etOwnerName.getText().toString())) {
                    AppUtils.showToast("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(etContacts.getText().toString())) {
                    AppUtils.showToast("请输入联系人姓名");
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    AppUtils.showToast("请输入电话号");
                    return;
                }
                if (etPhone.getText().toString().length() != 11) {
                    AppUtils.showToast("电话号不正确");
                    return;
                }

                addSignTwo.setTradingPayment(tvPayType.getText().toString());
                addSignTwo.setTradingVolRent(tvRent.getText().toString().trim());
                addSignTwo.setOwnerLandlordName(etOwnerName.getText().toString());
                addSignTwo.setOwnerContact(etContacts.getText().toString());
                addSignTwo.setOwnerPhone(etPhone.getText().toString());
//                String performanceId = SPUtils.getInstance(this).getString("performanceId", "");
                addSignTwo.setPerformanceId(String.valueOf(performanceId));
                Log.e("addSignTwo", new Gson().toJson(addSignTwo));
                mPresenter.saveTwoStep(addSignTwo);

                break;

        }
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);

        List<String> titles = new ArrayList<>();
        titles.add("老顾客及朋友介绍");
        titles.add("网络");
        titles.add("上门");
        titles.add("其他");
        checkBoxAdapter = new CheckBoxAdapter(getActivity());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        recycleView.setLayoutManager(gridLayoutManager);
        recycleView.setAdapter(checkBoxAdapter);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 3) {
                    return 3;
                }
                return 1;
            }
        });
        checkBoxAdapter.setNewData(titles);


        checkBoxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter.setSelect(position);
                addSignTwo.setOwnerSource(String.valueOf(position + 1));
            }
        });

    }

    @Override
    protected void loadData() {
       /* if (signType == 1) {

            addSignTwo.setOwnerSource("1");
        } else {
            mPresenter.getSingnContractdetail(performanceId);
        }*/
        mPresenter.getSingnContractdetail(performanceId);
    }

    @Override
    public void getPassengerNumberListSuccess(List<PassengerNumberBean> list) {

    }

    @Override
    public void saveOneStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveTwoStepSuccess(BaseData baseData) {
        SignStepActivity activity = (SignStepActivity) getActivity();
        activity.initFragment3();
    }

    @Override
    public void saveThreeStepSuccess(BaseData baseData) {

    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        addSignTwo.setOwnerSource("1");
        String[] s = signContractDetailBean.getTradingSignDate().split(" ");
        addSignTwo.setTradingSignDate(signContractDetailBean.getTradingSignDate());
        tvDate.setText(s[0]);
        addSignTwo.setTradingPayment(signContractDetailBean.getTradingPayment());
        tvPayType.setText(signContractDetailBean.getTradingPayment());
        addSignTwo.setTradingPaymentDate(signContractDetailBean.getTradingPaymentDate());

        tvJiaofangTime.setText(signContractDetailBean.getTradingPaymentDate().split(" ")[0]);
        tvRent.setText(signContractDetailBean.getTradingVolRent());
        addSignTwo.setTradingVolRent(signContractDetailBean.getTradingVolRent());
        tvStartTime.setText(signContractDetailBean.getTradingBeginTime().split(" ")[0]);
        tvEndTime.setText(signContractDetailBean.getTradingEndTime().split(" ")[0]);
        addSignTwo.setTradingBeginTime(signContractDetailBean.getTradingBeginTime());
        addSignTwo.setTradingEndTime(signContractDetailBean.getTradingEndTime());

        etOwnerName.setText(signContractDetailBean.getOwnerLandlordName());
        addSignTwo.setOwnerLandlordName(signContractDetailBean.getOwnerLandlordName());
        addSignTwo.setOwnerContact(signContractDetailBean.getOwnerContact());
        etContacts.setText(signContractDetailBean.getOwnerContact());
        addSignTwo.setOwnerPhone(signContractDetailBean.getOwnerPhone());
        etPhone.setText(signContractDetailBean.getOwnerPhone());
    }

    @Override
    public void exChangeSignContractSuccess(BaseData data) {

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog(TextView textView, int type) {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                textView.setText(year + "-" + month + "-" + dayOfMonth);
                if (type == 1) {
                    addSignTwo.setTradingSignDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 2) {
                    addSignTwo.setTradingPaymentDate(textView.getText().toString() + " 00:00:00");
                } else if (type == 3) {
                    addSignTwo.setTradingBeginTime(textView.getText().toString() + " 00:00:00");
                } else {
                    addSignTwo.setTradingEndTime(textView.getText().toString() + " 00:00:00");

                }
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();

    }

}